package entity

import (
	"fmt"
	"time"

	"gorm.io/gorm"
	"shqsoft.cn/pas3ui/base"
)

//ShIndiGjszsdr 中收（私行）
type ShIndiGjszsdr struct {
	JLID  int       `gorm:"AUTO_INCREMENT;primary_key;column:JLID"`
	CUID  int       `gorm:"column:CUID"`
	JYRQ  time.Time `gorm:"column:JYRQ"`
	CUNM  string    `gorm:"column:CUNM"`
	IDNO  string    `gorm:"column:IDNO"`
	CPNM  string    `gorm:"column:CPNM"`
	JE    float64   `gorm:"column:JE"`
	CSZS  float64   `gorm:"column:CSZS"`
	FPZS  float64   `gorm:"column:FPZS"`
	EMID  int       `gorm:"column:EMID"`
	EMNO  string    `gorm:"column:EMNO"`
	EMNM  string    `gorm:"column:EMNM"`
	PDNM  string    `gorm:"column:PDNM"`
	DRRQ  time.Time `gorm:"column:DRRQ"`
	CZRID int       `gorm:"column:CZRID"`
	CZR   string    `gorm:"column:CZR"`
	CZRQ  time.Time `gorm:"column:CZRQ"`
}

//TableName
func (ShIndiGjszsdr) TableName() string {
	return "SH_INDI_GJSZSDR"
}

// BeforeCreate
func (u *ShIndiGjszsdr) BeforeCreate(tx *gorm.DB) (err error) {
	var emid, cuid *int
	base.CHDB.Raw("SELECT ID FROM T_INDI_CLIE WHERE CUNM=?", u.CUNM).Row().Scan(&cuid)
	if cuid == nil {
		err = fmt.Errorf("客户名称%s在系统中不存在！", u.CUNM)
		return
	}
	tx.Raw(`select "ID" from "T_SysUser" where "SysUserID" = ?`, u.EMNO).Row().Scan(&emid)
	if emid == nil {
		err = fmt.Errorf("员工号%s在系统中不存在！", u.EMNO)
		return
	}
	u.EMID = *emid
	u.CZRQ = time.Now()
	return
}

//ShIndiGjszsdrEF 返回结构体
func ShIndiGjszsdrEF() base.EntityFunc {
	return func() interface{} {
		entity := ShIndiGjszsdr{}
		return &entity
	}
}

//ShIndiGjszsdrESF 返回结构体数组
func ShIndiGjszsdrESF() base.EntitiesFunc {
	return func() interface{} {
		var entities []ShIndiGjszsdr
		return &entities
	}
}

func init() {
	base.DaoPoolObj.Put("ShIndiGjszsdr", base.NewDefaultDao(ShIndiGjszsdrEF(), ShIndiGjszsdrESF(), nil, "my"))
}
